package com.hangkong.controller;//package com.heitian.hangkong.controller;

import com.aliyuncs.exceptions.ClientException;
import com.hangkong.model.torder;
import com.hangkong.service.FlightcabinService;
import com.hangkong.service.OrderService;
import com.hangkong.service.PassengerInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * oooooo
 */
//阿斯顿

@Controller
@RequestMapping("/order")
public class  OrderController {
//
    private Logger log = Logger.getLogger(OrderController.class);
    @Resource
    private OrderService orderService;
     @Resource
     private PassengerInfoService passengerInfoService;

    @Resource
    private FlightcabinService flightcabinService;

    @RequestMapping("/showOrder")
    @ResponseBody
    public List<torder> showOrder(HttpServletRequest request){
        log.info("查询所有订单信息");
        List<torder> orderList = orderService.getAllOrder();
      return orderList;

    }

    @RequestMapping("/deleteOrder")
    @ResponseBody
    public int deleteOrder(@RequestParam(value = "deleteArray[]")String[] deleteArray){
        log.info("删除订单");

        int t = 0;
        for (String id:deleteArray){
            t =  orderService.DeleteOrder(id);
        }
        return t;

    }

//出票方法
    @RequestMapping("/changeTkt")
    @ResponseBody
    public int changeTkt(HttpServletRequest request){
        log.info("出票");
        String tktid = request.getParameter("tktid");
        int changeOk = orderService.changeTkt(tktid);
        return changeOk;

    }


    @RequestMapping("/queryOrderAll")
    @ResponseBody
    public torder queryOrderAll(HttpServletRequest request){
        log.info("查询订单详情");
        String id=  request.getParameter("queryOrderAllid");

        torder orderList = orderService.queryOrderAll(id);
        return orderList;

    }
    @RequestMapping("/condition_query")
    @ResponseBody
    public List<torder> condition_query(HttpServletRequest request){
        log.info("条件查询订单详情");
      String  select_flightno =request.getParameter("select_flightno" );
      String  select_deptcity=request.getParameter("select_deptcity" );
      String  select_arrcity=request.getParameter("select_arrcity" );
      String  select_userid=request.getParameter("select_userid" );
      String  select_linkman=request.getParameter("select_linkman" );
      String  select_linkphone=request.getParameter("select_linkphone" );
      String  select_flightdate=request.getParameter("select_flightdate" );
       List<torder> orderList = orderService.condition_query(select_flightno,select_deptcity,select_arrcity,select_userid
       ,select_linkman,select_linkphone,select_flightdate);
        System.out.printf(orderList.toString());

        return orderList;

    }

    //增加订单方法
    @RequestMapping("/AddBooking")
    @ResponseBody
    public torder AddBooking(@RequestParam(value = "arrayName[]")String[] arrayName,
                              @RequestParam(value = "arrayCode[]")String[] arrayCode,
                              @RequestParam(value = "arrayZjlx[]")String[] arrayZjlx,
                              @RequestParam(value = "booking_linkInfo_name") String booking_linkInfo_name,
                              @RequestParam(value = "booking_linkInfo_phone") String booking_linkInfo_phone,
                              @RequestParam(value = "booking_linkInfo_email") String booking_linkInfo_email,
                              @RequestParam(value = "deptCity") String deptCity,
                              @RequestParam(value = "arrCity") String arrCity,
                              @RequestParam(value = "flightprice") double flightprice,
                              @RequestParam(value = "flightNo") String flightNo,
                             @RequestParam(value = "flightDate_Arr_Date") String flightDate_Arr_Date,
//                            @RequestParam(value = "flightDate_Arr_Time") String flightDate_Arr_Time,
                             @RequestParam(value = "flightCabin") String flightCabin) {
//                            @RequestParam(value = "flightDate_Dept_Date") String flightDate_Dept_Date,
//                            @RequestParam(value = "flightDate_Dept_Time") String flightDate_Dept_Time,
        log.info("增加订单方法");
        java.util.Date date = new java.util.Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        Timestamp flightDate_Dept_Date = timeStamp;
        Timestamp Create_Date = timeStamp;
        torder order = new torder();
        order.setFlightno(flightNo);
        order.setArrcity(arrCity);
        order.setDeptcity(deptCity);
        order.setLinkman(booking_linkInfo_name);
        order.setLinkphone(booking_linkInfo_phone);
        order.setFlightcabin(flightCabin);
        order.setFlightdate(flightDate_Dept_Date);
        order.setCreatetime(Create_Date);
        order.setPrice(flightprice);
        String maxId = orderService.selectId();
//        String orderId = String.valueOf(Integer.parseInt(maxId) + 1);
//        生成时间戳 毫秒
        Long time = System.currentTimeMillis();
//        生成三位数随机数
        int k=(int)(Math.random()*900)+100;
        String orderId = String.valueOf(time)+String.valueOf(k);
        order.setId(orderId);
        Integer type = null;
        Integer value = orderService.insertOrder(order);
        for (int i = 0; i < arrayCode.length; i++) {
            type = passengerInfoService.insertPassenger(arrayName[i], arrayZjlx[i], arrayCode[i], orderId, timeStamp);
        }
          Integer i=  flightcabinService.subflightcabin(flightNo,flightCabin);
            return order;


    }

    @RequestMapping("/showUserOrder")
    @ResponseBody
    public torder showUserOrder(HttpServletRequest request){
        log.info("显示前台订单信息页面");
            String id = request.getParameter("orderId");
        torder orderList = orderService.showUserOrder(id);
        return orderList;

    }
    @RequestMapping("/passengInfo")
    @ResponseBody
    public List<torder> passengInfo(HttpServletRequest request){
        log.info("显示前台订单用户页面");
        String orderId = request.getParameter("orderId");
        List<torder> orderList = orderService.passengInfo(orderId);
        return orderList;

    }
    @RequestMapping("/smsVerify")
    @ResponseBody
    public String smsVerify(HttpServletRequest request)throws ClientException {
        log.info("手机号验证码验证");
      String  phone= request.getParameter("phone");
           String smsyan= SmsVerify.sms(phone);
            return smsyan;

    }
    @RequestMapping("/smsVerifyJudge")
    @ResponseBody
    public Integer smsVerifyJudge(HttpServletRequest request){
        log.info("手机号验证码验证");
        String  getyanzhengma= request.getParameter("getyanzhengma");
        String  dataPhone= request.getParameter("dataPhone");
       if (getyanzhengma.equals(dataPhone)){
           return 1;
       }else{
           return 0;
       }
    }


}
